<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript">
        var setting = {
            async: {
                enable: true,
                url: "/menu/getZv",
                autoParam: ["id", "name"]
            },
            data:{
                simpleData:{
                    enable: true
                }
            },
            check: {
                enable: true,
                chkStyle: "checked"
            },
            view : {
                addHoverDom: function(treeId, treeNode){
                    var aObj = $("#" + treeNode.tId + "_a");
                    if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                    var s = '<span id="btnGroup'+treeNode.tId+'">';
                    if ( treeNode.level == 0 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="addNode('+treeNode.id+')" href="#" >添加</a>';

                    } else if ( treeNode.level == 1 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        if (treeNode.children == undefined) {
                            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
                        }
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                    } else if ( treeNode.level == 2 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
                    }
                    s += '</span>';
                    aObj.after(s);
                },
                removeHoverDom: function(treeId, treeNode){
                    $("#btnGroup"+treeNode.tId).remove();
                }
            }
        };



        function addNode(di) {
            BootstrapDialog.show({
                title: '添加',
                message: $('<div></div>').load('/menu/toAdd'),
                buttons: [{
                    label: '取消',
                    action: function(dialog) {
                        dialog.close();
                    }
                }, {
                    label: 'OK',
                    action: function(dialog) {
                        $.ajax({
                            url:"/menu/addInfo",
                            type:"post",
                            dataType:"json",
                            data:$("#addform").serialize() + "&parentid=" + di,
                            success:function (data) {
                                alert("成功")
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            },
                            error:function (data){

                                alert("gfhhdgjsahgja")
                            }
                        })
                    }
                }]
            });
        }
        function editNode(id) {
            BootstrapDialog.show({
                title: '修改',
                message: $('<div></div>').load('/menu/toupdate/'+id),
                buttons: [{
                    label: '取消',
                    action: function(dialog) {
                        dialog.close();
                    }
                }, {
                    label: 'OK',
                    action: function(dialog) {
                        $.ajax({
                            url:"/menu/updateInfo",
                            type:"post",
                            dataType:"json",
                            data:$("#updateform").serialize(),
                            success:function (data) {
                                alert("成功")
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            },
                            error:function (data){

                                alert("gfhhdgjsahgja")
                            }
                        })
                    }
                }]
            });
        }
        function deleteNode(id) {
            if(confirm("确定删除吗")){
                $.ajax({
                    url:"/menu/deleteInfo/"+ id,
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        if (data == 1){

                        }


                    },
                    error:function (data){

                        $.fn.zTree.init($("#treeDemo"), setting);
                    }
                })

            }
        }
        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting);
        });
    </script>
</head>
<body>
<div th:insert="/commen/commen.html"></div>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>

</div>

</body>
</html>